import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class TestJDBCInsert {
    public static void main(String[] args) throws SQLException {
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入id");
        int id=scanner.nextInt();
        System.out.println("请输入名字");
        String name=scanner.next();
        //找到数据源
        DataSource dataSource=new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java110?characterEncoding=utf8&useSSl=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("123456");


        //建立连接
        Connection connection=dataSource.getConnection();

        //构造一个sql
        String sql="insert into student values(?,?)";
        PreparedStatement statement=connection.prepareStatement(sql);
        statement.setInt(1,id);
        statement.setString(2,name);
        //上传
        int n=statement.executeUpdate();
        System.out.println("n="+n);

        //释放资源
        statement.close();
        connection.close();
    }
}
